/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tradeplus.admin;

import com.tradeplus.admin.model.City;
import com.tradeplus.admin.model.Country;
import com.tradeplus.admin.model.State;
import com.tradeplus.admin.service.AdminDeskService;
import com.tradeplus.admin.service.AdminGroupService;
import com.tradeplus.admin.service.AdminPrivilegeService;
import com.tradeplus.admin.service.AdminService;
import com.tradeplus.admin.service.AdminUserService;
import com.tradeplus.admin.service.KernalService;
import com.tradeplus.admin.service.PreferenceService;
import com.tradeplus.admin.service.ProfileService;
import com.tradeplus.security.model.User;
import com.tradeplus.security.service.UserDetailsService;
import com.tradeplus.util.JSONView;
import com.tradeplus.util.TPLController;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author ronnie
 */
public class AdminController extends TPLController {
    
    private AdminService adminService;
    private UserDetailsService userDetailsService;
    /*private AdminUserService adminUserService;
    private AdminDeskService adminDeskService;
    private AdminGroupService adminGroupService;
    private AdminPrivilegeService adminPrivilegeService;
    private KernalService kernalService;
    private PreferenceService preferenceService;
    private ProfileService profileService;*/

    /*public void setAdminDeskService(AdminDeskService adminDeskService) {
        this.adminDeskService = adminDeskService;
    }

    public void setAdminGroupService(AdminGroupService adminGroupService) {
        this.adminGroupService = adminGroupService;
    }

    public void setAdminPrivilegeService(AdminPrivilegeService adminPrivilegeService) {
        this.adminPrivilegeService = adminPrivilegeService;
    } */

    public void setUserDetailsService(UserDetailsService userDetailsService) {
		this.userDetailsService = userDetailsService;
	}

	public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    /*public void setAdminUserService(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    public void setKernalService(KernalService kernalService) {
        this.kernalService = kernalService;
    }

    public void setPreferenceService(PreferenceService preferenceService) {
        this.preferenceService = preferenceService;
    }

    public void setProfileService(ProfileService profileService) {
        this.profileService = profileService;
    }*/
    
	public ModelAndView CreateUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		User user = new User();
		user.setUsername(request.getParameter("username"));
		user.setEncryptPassword(request.getParameter("username"), request.getParameter("password"));
		userDetailsService.save(user);
		HashMap map = new HashMap();
		map.put("new", "user");
		return new ModelAndView(new JSONView(),map);
	}
    /**
     * Admin Login View open
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
    public ModelAndView AdminLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("/admin/login",new HashMap());
    }
    
    /**
     * Load Admin Profile to view in JavaScript
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
    public ModelAndView LoadAdminProfile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("/admin/desk",new HashMap());
    }
    
    /**
     * insert country and return the mapped values in the JSON
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
    public ModelAndView insertCountry(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	Country country = new Country();
    	country.setCountry(request.getParameter("country"));
    	country.setActive(true);
    	adminService.saveCountry(country);
    	HashMap countryMap = new HashMap();
    	countryMap.put("newCountry", request.getParameter("country"));
    	
        return new ModelAndView(new JSONView(), countryMap);
    }
    
    /**
     * insert state and return the mapped values in the JSON
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
    public ModelAndView insertState(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	State state = new State();
    	state.setState(request.getParameter("state"));
    	Country country = adminService.loadCountryById(Long.parseLong(request.getParameter("cid")));
    	state.setCountry(country);
    	state.setActive(true);
    	adminService.saveState(state);
    	HashMap stateMap = new HashMap();
    	stateMap.put("newState", request.getParameter("state"));
    	
        return new ModelAndView(new JSONView(), stateMap);
    }
    
    /**
     * insert city and return the mapped values in the JSON
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
    public ModelAndView insertCity(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	City city = new City();
    	city.setCity(request.getParameter("city"));
    	State state = adminService.loadStateById(Long.parseLong(request.getParameter("sid")));
    	city.setState(state);
    	city.setActive(true);
    	adminService.saveCity(city);
    	HashMap cityMap = new HashMap();
    	cityMap.put("newState", request.getParameter("state"));
    	
        return new ModelAndView(new JSONView(), cityMap);
    }
}
